/**
 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

export function deepEqual(x, y) {
    // Approximate comparison for objects imported from ets, fails for some corner cases
    if (x === y) {
        return true;
    } else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
        if (Object.getPrototypeOf(x) !== Object.getPrototypeOf(y)) {
            return false;
        }
        // Ets object fields are stored in prototype, but we cannot get them
        // with Object.getPrototypeOf(x)[property]
        for (let prop in x) {
            if (Object.hasOwn(x, prop) != Object.hasOwn(y, prop)) {
                return false;
            }
            if (!deepEqual(x[prop], y[prop])) {
                return false;
            }
        }
        // Check if y does not have properties not present in x
        for (let prop in y) {
            if (y[prop] !== undefined && x[prop] === undefined) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}

function JsClass(value) {
    this.int_value = value;
}

let const_obj = new JsClass(1);
function create_obj() {
    return new JsClass(1);
}
function create_null_obj() {
    return null;
}

% @type_infos.each do |type_info|
% fn_name = "js_get_#{type_info.name}"
export let js_get_<%= type_info.name %> = function() {
    return <%= type_info.value %>;
}

% end

var saved_values = [undefined, undefined];

export let js_set = function(index, value) {
    saved_values[index] = value;
    return index;
}

export let js_check_set = function(index, value) {
    return deepEqual(saved_values[0], saved_values[1]) ? 0 : 1;
}
